function reverse(str) {
    strs = str.trim().split(' ')
    let res = ''
    for (let i = 0; i< strs.length; i++) {
        if (strs[i] == '') {
            continue
        }
        res += helper(strs[i])
        if (i != strs.length -1) {
            res += ' '
        }
    }
    function helper(str) {
        let temp = ''
        for (let j = str.length- 1 ; j >= 0; j--) {
            temp += str[j]
        }
        return temp
    }
    return res
}
let str = "  i like    apple "
console.log(reverse(str))